﻿using Lamex.Base.Framework.BaseDal;
using Lamex.Common;
using Lamex.SystemManager.Interfaces;
using Lamex.SystemManager.Model;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Lamex.SystemManager.DalServer
{
    public class Sys_Function : BaseDalSQL<Sys_FunctionInfo>, ISys_Function
    {
        /// <summary>
        /// 获取树形结构的功能列表
        /// </summary>
        private bool isDescending = false;//是否以降序排列
        public List<SysFunctionNodeInfo> GetFuctionList(string Type)
        {
            string condition = !string.IsNullOrEmpty(Type) ? string.Format("Where SystemType='{0}' ", Type) : "";
            List<SysFunctionNodeInfo> arrReturn = new List<SysFunctionNodeInfo>();
            string sql = string.Format("Select * From {0} {1} Order By PID, FunName ", tableName, condition);

            DataTable dt = base.GetDataTableBySql(sql);
            string sortCode = string.Format("{0} {1}", "SortCode", isDescending ? "DESC" : "ASC");
            DataRow[] dataRows = dt.Select(string.Format(" PID = '{0}' ", -1), sortCode);
            List<Sys_FunctionInfo> list = base.GetEntriyList(" 1=1 ");
            for (int i = 0; i < dataRows.Length; i++)
            {
                string id = dataRows[i]["ID"].ToString();
                SysFunctionNodeInfo menuNodeInfo = GetNode(id, dt, list);
                arrReturn.Add(menuNodeInfo);

                //SysFunctionNodeInfo menuNodeInfo = ConvertHelper.DataRowToEntity<SysFunctionNodeInfo>(dataRows[i]);
                //GetNode(menuNodeInfo, dt);
                //arrReturn.Add(menuNodeInfo);

            }

            return arrReturn;
        }
        private SysFunctionNodeInfo GetNode(string id, DataTable dt, List<Sys_FunctionInfo> list)
        {
            Sys_FunctionInfo menuInfo = list.Where(p => p.ID == id).FirstOrDefault();
            SysFunctionNodeInfo menuNodeInfo = null;
            if (menuInfo != null)
                menuNodeInfo = new SysFunctionNodeInfo(menuInfo);

            string sortCode = string.Format("{0} {1}", "SortCode", isDescending ? "DESC" : "ASC");
            DataRow[] dChildRows = dt.Select(string.Format(" PID='{0}'", id), sortCode);

            for (int i = 0; i < dChildRows.Length; i++)
            {
                string childId = dChildRows[i]["ID"].ToString();
                SysFunctionNodeInfo childNodeInfo = GetNode(childId, dt, list);
                menuNodeInfo.Children.Add(childNodeInfo);
            }
            return menuNodeInfo;

        }

        //private void GetNode(SysFunctionNodeInfo parentNode, DataTable dt)
        //{
        //    string sortCode = string.Format("{0} {1}", "SortCode", isDescending ? "DESC" : "ASC");
        //    DataRow[] dChildRows = dt.Select(string.Format(" PID='{0}'", parentNode.ID), sortCode);

        //    for (int i = 0; i < dChildRows.Length; i++)
        //    {
        //        //string childId = dChildRows[i]["ID"].ToString();
        //        SysFunctionNodeInfo childNodeInfo = ConvertHelper.DataRowToEntity<SysFunctionNodeInfo>(dChildRows[i]);
        //        GetNode(childNodeInfo , dt);
        //        parentNode.Children.Add(childNodeInfo);

        //    }
        //}

        public List<Sys_FunctionInfo> GetFunctionRoleList(int SqlWhere)
        {
            string sql = @"SELECT a.* FROM dbo.Sys_Function a LEFT JOIN   [Sys_RoleFunction] b ON a.ID=b.FunctionId WHERE b.RoleId='" + SqlWhere + "'";
            DataTable dt = GetDataTableBySql(sql);
            return Lamex.Common.ConvertHelper<Sys_FunctionInfo>.DataTableToList(dt);
        }
    }
}
